package com.tanhua.server.service;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.common.mapper.FreezeMapper;
import com.tanhua.common.pojo.UserFreeze;
import com.tanhua.dubbo.server.api.OperationApi;
import com.tanhua.dubbo.server.enums.DescriptionEnum;
import com.tanhua.dubbo.server.enums.OperatingEnum;
import com.tanhua.dubbo.server.pojo.Operation;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.ViewVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class OperationService {

    @Autowired
    private FreezeMapper freezeMapper;


    @Reference(version = "1.0.0")
    private OperationApi operationApi;

    public PageResult getOperations(Integer page, Integer pageSize) {

        /*String username = BackToken.getUsername();
        if (!StrUtil.isNotEmpty(username)) {
            return null;
        }*/

        PageResult pageResult = new PageResult();
        pageResult.setPage(page);
        pageResult.setPagesize(pageSize);

        List<Operation> operations = operationApi.getOperations(page, pageSize, Operation.class);

        pageResult.setItems(operations);


        return pageResult;

    }

    public void addMongo() {
        Boolean aBoolean = operationApi.queryOperation(OperatingEnum.PASS, 122456L, DescriptionEnum.AVATAR);
        operationApi.queryOperation(OperatingEnum.PASS, 124456L, DescriptionEnum.AVATAR);
        operationApi.queryOperation(OperatingEnum.DELETE, 125456L, DescriptionEnum.DYNAMIC);
        operationApi.queryOperation(OperatingEnum.FAIL, 126456L, DescriptionEnum.AVATAR);
        operationApi.queryOperation(OperatingEnum.DELETE, 127456L, DescriptionEnum.VIDEO);
        operationApi.queryOperation(OperatingEnum.PASS, 128456L, DescriptionEnum.AVATAR);
        operationApi.queryOperation(OperatingEnum.PASS, 129456L, DescriptionEnum.VIDEO);
        operationApi.queryOperation(OperatingEnum.FAIL, 121046L, DescriptionEnum.VIDEO);
        operationApi.queryOperation(OperatingEnum.PASS, 121356L, DescriptionEnum.AVATAR);
        operationApi.queryOperation(OperatingEnum.PASS, 123356L, DescriptionEnum.DYNAMIC);
        System.out.println("结果是：" +aBoolean);
    }

    public ViewVo getView(Map<String, String> param) {
        if (param.isEmpty()){
            return null;
        }
        try {
            Long operatingId = Long.valueOf(param.get("operatingId"));
            String username = param.get("username");
            String ip = param.get("ip");
            Long date = Long.valueOf(param.get("created"));
            String range = param.get("freezingRange");

            if (!(ObjectUtil.isNotEmpty(operatingId)&&ObjectUtil.isNotEmpty(username)&&
                    ObjectUtil.isNotEmpty(ip)&&ObjectUtil.isNotEmpty(date)&&ObjectUtil.isNotEmpty(range))){
                return null;
            }

            ViewVo viewVo = new ViewVo();
            viewVo.setCreated(date);
            viewVo.setIp(ip);
            viewVo.setUsername(username);
            viewVo.setOperatingId(operatingId);

            QueryWrapper<UserFreeze> query = new QueryWrapper<>();
            query.eq("user_id",operatingId);
            UserFreeze userFreeze = freezeMapper.selectOne(query);
            if (ObjectUtil.isNotEmpty(userFreeze)){
                Integer value = userFreeze.getFreezingTime();
                viewVo.setFreezingTime(value == 1 ? "冻结3天" : (value == 2 ?"冻结7天" : "永久冻结"));
                Integer value1 = userFreeze.getFreezingRange();
                viewVo.setFreezingRange(value1 == 1 ? "冻结登录" : (value == 2 ?"冻结发言" : "冻结发布动态"));
                viewVo.setReasonsForFreezing(userFreeze.getReasonsForFreezing());
                return viewVo;
            }
            viewVo.setReasonsForFreezing(range);


            return viewVo;
        } catch (NumberFormatException e) {
            log.error("查询用户冻结失败" + e);
        }
        return null;
    }

    public PageResult queyOperations(Map<String, String> param) {
        if (param.isEmpty()){
            return null;
        }
        String username = param.get("username");
        String content = param.get("content");
        Integer page = Integer.valueOf(param.get("page"));
        Integer pageSize = Integer.valueOf(param.get("pageSize"));
        String date = param.get("date");
        DateTime start = DateUtil.parse(date);
        long startTime = start.getTime();
        String endTimes = param.get("endTime");
        DateTime end = DateUtil.parse(endTimes);
        long endTime = end.getTime();
        PageResult pageResult = new PageResult();

        if (ObjectUtils.isEmpty(username)&&ObjectUtils.isEmpty(content)
                &&ObjectUtils.isEmpty(startTime)&&ObjectUtils.isEmpty(endTime)){
            pageResult = getOperations(1, 5);
            return pageResult;
        }else {
            List<Operation> operations = operationApi.queryPageOperations(username,content,startTime,endTime,page,pageSize);

            pageResult.setPagesize(pageSize);
            pageResult.setPage(page);
            pageResult.setItems(operations);
        }
        return pageResult;
    }
}
